package au.com.handsunited.web.validator.organisation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.validators.entity.OrganisationValidator;
import au.com.handsunited.web.command.organisation.OrganisationCommand;
import au.com.handsunited.web.validator.project.ProjectCommandValidator;

/**
 * @author Arun Chandrasekhar
 *
 */
public class OrganisationCommandValidator implements Validator {

	private final OrganisationValidator organisationValidator;

	private static Logger logger = LoggerFactory.getLogger(ProjectCommandValidator.class);


	/**
	 *
	 */
	public OrganisationCommandValidator(OrganisationValidator organisationValidator) {
		this.organisationValidator = organisationValidator;
	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(OrganisationCommand.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {
		OrganisationCommand organisationCommand = (OrganisationCommand) target;
		Organisation organisation = organisationCommand.getOrganisation();

		try {
			errors.pushNestedPath("organisation");
			ValidationUtils.invokeValidator(organisationValidator, organisation, errors);
		} finally {
			errors.popNestedPath();
		}

	}



}
